import axios from 'axios';
import gconfig from '@/config/global_config'

// GET 请求
export async function get(url, params = {}, config = {}) {

    //let needAuthentication = config.needAuthentication

    try {
        url = gconfig.constants.API_ORIGIN + url;
        const response = await axios.get(url, {
            params,
            ...config,
        });
        return response.data;
    } catch (error) {
        console.error(error);
        throw error;
    }
}
// POST 请求
export async function post(url, data, contentType = 'json', config = {}) {
    url = gconfig.constants.API_ORIGIN + url;
    const headers = {
        'Content-Type':
            contentType === 'json'
                ? 'application/json'
                : 'application/x-www-form-urlencoded',
        ...config.headers,
    };
    if (contentType !== 'json') {
        const qs = require('qs');
        data = qs.stringify(data);
    }
    try {
        const response = await axios.post(url, data, { ...config, headers });
        //console.log(response.data)
        return response.data;
    } catch (error) {
        console.error(error);
        throw error;
    }
}

export async function getWithNginx(url, params = {}, config = {}) {
    try {
        url = gconfig.constants.API_NGINX_ORIGIN + url;
        const response = await axios.get(url, {
            params,
            ...config,
        });
        return response.data;
    } catch (error) {
        console.error(error);
        throw error;
    }
}

// 原生请求； 不设置header，则是application/json方式传参
      // const options = {
      //   method: 'POST',
      //   headers: { 'content-type': 'application/x-www-form-urlencoded' },
      //   data: qs.stringify(reqParam),
      //   url,
      // };
      // axios(options).then(response => {
      //       this.curlVal = response.data.data;
      //     });